package com.ruoyi.basebill.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.basebill.domain.OmsSumpurinwarehsbillentry;
import com.ruoyi.basebill.mapper.OmsSumpurinwarehsbillMapper;
import com.ruoyi.basebill.domain.OmsSumpurinwarehsbill;
import com.ruoyi.basebill.service.IOmsSumpurinwarehsbillService;

/**
 * 汇总采购入库单Service业务层处理
 * 
 * @author wenyixiong
 * @date 2023-02-18
 */
@Service
public class OmsSumpurinwarehsbillServiceImpl implements IOmsSumpurinwarehsbillService 
{
    @Autowired
    private OmsSumpurinwarehsbillMapper omsSumpurinwarehsbillMapper;
    @Autowired
    private OmsSumpurinwarehsbillServiceImpl sumpurinwarehsbillService;

    /**
     * 查询汇总采购入库单
     * 
     * @param id 汇总采购入库单主键
     * @return 汇总采购入库单
     */
    @Override
    public OmsSumpurinwarehsbill selectOmsSumpurinwarehsbillById(Long id)
    {
        return omsSumpurinwarehsbillMapper.selectOmsSumpurinwarehsbillById(id);
    }

    /**
     * 查询汇总采购入库单列表
     * 
     * @param omsSumpurinwarehsbill 汇总采购入库单
     * @return 汇总采购入库单
     */
    @Override
    public List<OmsSumpurinwarehsbill> selectOmsSumpurinwarehsbillList(OmsSumpurinwarehsbill omsSumpurinwarehsbill)
    {
        return omsSumpurinwarehsbillMapper.selectOmsSumpurinwarehsbillList(omsSumpurinwarehsbill);
    }

    @Override
    public List<OmsSumpurinwarehsbill> selectOmsSumpurinwarehsbillDetailList(OmsSumpurinwarehsbill omsSumpurinwarehsbill) {
        return omsSumpurinwarehsbillMapper.selectOmsSumpurinwarehsbillDetailList(omsSumpurinwarehsbill);
    }

    /**
     * 新增汇总采购入库单
     * 
     * @param omsSumpurinwarehsbill 汇总采购入库单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertOmsSumpurinwarehsbill(OmsSumpurinwarehsbill omsSumpurinwarehsbill)
    {
        int rows = omsSumpurinwarehsbillMapper.insertOmsSumpurinwarehsbill(omsSumpurinwarehsbill);
        sumpurinwarehsbillService.insertOmsSumpurinwarehsbillentry(omsSumpurinwarehsbill);
        return rows;
    }

    /**
     * 修改汇总采购入库单
     * 
     * @param omsSumpurinwarehsbill 汇总采购入库单
     * @return 结果
     */
    @Transactional
    @Override
    public int updateOmsSumpurinwarehsbill(OmsSumpurinwarehsbill omsSumpurinwarehsbill)
    {
        //传入汇总采购入库单不存在分录则不用修改分录信息只修改表头信息
        if(omsSumpurinwarehsbill.getOmsSumpurinwarehsbillentryList() != null && omsSumpurinwarehsbill.getOmsSumpurinwarehsbillentryList().size() != 0) {
            omsSumpurinwarehsbillMapper.deleteOmsSumpurinwarehsbillentryByParentid(omsSumpurinwarehsbill.getId());
            sumpurinwarehsbillService.insertOmsSumpurinwarehsbillentry(omsSumpurinwarehsbill);
        }
        return omsSumpurinwarehsbillMapper.updateOmsSumpurinwarehsbill(omsSumpurinwarehsbill);
    }

    /**
     * 批量删除汇总采购入库单
     * 
     * @param ids 需要删除的汇总采购入库单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteOmsSumpurinwarehsbillByIds(Long[] ids)
    {
        omsSumpurinwarehsbillMapper.deleteOmsSumpurinwarehsbillentryByParentids(ids);
        return omsSumpurinwarehsbillMapper.deleteOmsSumpurinwarehsbillByIds(ids);
    }

    /**
     * 删除汇总采购入库单信息
     * 
     * @param id 汇总采购入库单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteOmsSumpurinwarehsbillById(Long id)
    {
        omsSumpurinwarehsbillMapper.deleteOmsSumpurinwarehsbillentryByParentid(id);
        return omsSumpurinwarehsbillMapper.deleteOmsSumpurinwarehsbillById(id);
    }

    /**
     * 新增汇总采购入库单明细信息
     * 
     * @param omsSumpurinwarehsbill 汇总采购入库单对象
     */
    public void insertOmsSumpurinwarehsbillentry(OmsSumpurinwarehsbill omsSumpurinwarehsbill)
    {
        List<OmsSumpurinwarehsbillentry> omsSumpurinwarehsbillentryList = omsSumpurinwarehsbill.getOmsSumpurinwarehsbillentryList();
        Long id = omsSumpurinwarehsbill.getId();
        if (StringUtils.isNotNull(omsSumpurinwarehsbillentryList))
        {
            List<OmsSumpurinwarehsbillentry> list = new ArrayList<OmsSumpurinwarehsbillentry>();
            for (OmsSumpurinwarehsbillentry omsSumpurinwarehsbillentry : omsSumpurinwarehsbillentryList)
            {
                omsSumpurinwarehsbillentry.setParentid(id);
                list.add(omsSumpurinwarehsbillentry);
            }
            if (list.size() > 0)
            {
                omsSumpurinwarehsbillMapper.batchOmsSumpurinwarehsbillentry(list);
            }
        }
    }

    /**
     * 查询是否推送过EAS的汇总采购入库单数据
     * @param isTrans
     * @return
     */
    public List<OmsSumpurinwarehsbill> selectPushEASSumPurinwarehsbillList(int isTrans){
        return omsSumpurinwarehsbillMapper.selectPushEASSumPurinwarehsbillList(isTrans);
    }
}
